<?php
if (!defined("__WD__")) exit("Wrong Connect");

/*
 * &getInstance( name:string ):Object
 * Create Singleton instance.
 *
 **/

function &getInstance( $name ) {
	static $instance;
	if ( !isset( $instance[$name] ) ) {
		$instance[$name] = new $name;
	}
	return $instance[$name];
}



/*
 * go( url:string )
 * go location.
 *
 **/

function go( $url ) { header("Location:".$url);exit; }



/*
 * p( val:mixed )
 * view print_r
 *
 **/

function p( $val ) {
	$src = array("<", ">");
	$chg = array("&lt;", "&gt;");
	echo "<pre>";
	echo str_replace($src, $chg, print_r($val, true));
	echo "</pre>";
}



/*
 * caseChange( input:string, output:string, text:string ):string
 * text를 input타입으로 받았을 때, output으로 처리합니다.
 *
 **/

function caseChange( $input, $output, $text ) {
	switch($input) {
		case "camel" :
		case "pascal" :
			$changer = explode(":", preg_replace("/([A-Z])/", ":$1", $text));
			break;
		case "underscore" :
		case "underbar" :
			$changer = explode("_", $text);
			break;
		case "space" :
			$changer = explode(" ", $text);
			break;
		case "dot" :
			$changer = explode(".", $text);
			break;
	}
	
	$o = array();
	foreach ($changer as $value) if ($value != "") array_push($o, strtolower($value));
	
	switch($output) {
		case "camel" :
			foreach($o as $key => $value) if ($key != 0) $o[$key] = ucwords($value);
			return implode("", $o);
		case "pascal" :
			foreach($o as $key => $value) $o[$key] = ucwords($value);
			return implode("", $o);
		case "underscore" :
		case "underbar" :
			return implode("_", $o);
		case "space" :
			return implode(" ", $o);
		case "dot" :
			return implode(".", $o);
	}

}



/*
 * setHeader( type:string, encoding:string )
 * 헤더를 정의 합니다.
 *
 **/

function setHeader($type = "html", $encoding = "utf-8") {
	switch($type) {
		case "html" :
		case "xml" :
		case "css" :
			header("Content-Type: text/{$type};charset={$encoding}");
			break;
		case "js" :
			header("Content-type: text/javascript;charset={$encoding}");
			break;
		case "json" :
			header("Content-Type: application/json;charset={$encoding}");
			break;
	}

}

/*
 * alert( msg:string )
 * 경고창을 띄우고 종료합니다.
 *
 **/
function alert( $msg, $url = null ) {
	$msg = htmlspecialchars($msg);
	echo "<script>alert(\"{$msg}\");".( (is_null($url))?"history.back();":"location.href=\"{$url}\"" )."</script>";
	exit;
}

/*
 * _unserialize( context:string ):mixed
 * context를 받아서 unserialize 하는데, null은 제거한다.
 *
 **/
function _unserialize( $context ) {
	
	if ( is_null($context) || $context == "" ) return null;
	
	$ret = unserialize($context);
	
	if ($ret === false) return null;
	
	foreach($ret as $k => $v) { if ( is_null($v) ) unset($ret[$k]); }
	
	if ( count($ret) == 0 ) return null;

	return $ret;
	
}


